<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Session;

class FinanceController extends Controller {

      /*
       * 报表
       * */
      public function report_list(Request $request){

         if(Session::get('role_id') == 1 ) {
             $servicer = DB::table('servicer')
                 ->where('status',0)
                 ->where('company_id','like','%'.$request->company_id.'%')
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code','company_id')
                 ->get();
         }else if(Session::get('role_id') == 2 ) {
             $servicer = DB::table('servicer')
                 ->where('company_id',Session::get('session_id'))
                 ->where('status',0)
                 ->where('company_id','like','%'.$request->company_id.'%')
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code','company_id')
                 ->get();
         }

           $servicer = json_decode($servicer,1);
           $company_id = [];
           foreach ($servicer as $key=>$value){
                 if(!in_array($value['company_id'],$company_id)){
                     $company_id[] = $value['company_id'];
                 }

                 $servicer[$key]['face_url'] = '/face_url/'.$value['face_url'];
                 $servicer[$key]['order_number'] = DB::table('order')->where('servicer_id',$value['id'])->count();
                 $servicer[$key]['total_sell'] = DB::table('order')->where('servicer_id',$value['id'])->sum('total_sell');
                 $servicer[$key]['already_pay'] = DB::table('order')->where('servicer_id',$value['id'])->sum('already_pay');
                 $servicer[$key]['percentage_money'] = DB::table('order_servicer_percentage')->where('servicer_id',$value['id'])->sum('percentage_money');
                 $servicer[$key]['branches_percentage_money'] = DB::table('order_servicer_percentage')
                     ->where('type',1)
                     ->where('servicer_id',$value['id'])
                     ->sum('percentage_money');
           }

           $company_name = DB::table('company')->select('name')->whereIn('id',$company_id)->get();
           return view('admin/finance/report_list',['servicer'=>$servicer,'company_name'=>$company_name,'company_id'=>$company_id]);
      }

      /*
       * 绩效考核
       * */

      public function performan_check(Request $request){

            //Redis::rpush('list1', 'ab1');
            //var_dump(Redis::lpop('list1'));

           if(Session::get('role_id') == 1 ) {
               $servicer = DB::table('servicer')
               ->select('id')
               ->get();

           }else if(Session::get('role_id') == 2 ){
                $servicer = DB::table('servicer')
               ->select('id')
               ->where('company_id',Session::get('session_id'))->get();
            }



           $servicer_id = [];
           foreach ($servicer as $key=>$value){
               $servicer_id[] = $value->id;
           }

           $order = DB::table('order')
               ->select('id','servicer_name','order_product_id','driver_id','driver_name','driver_get_date','arriver_date','supplyer_company','total_sell','already_pay')
               ->where('driver_status','>=',1)
               ->where('servicer_name','like','%'.$request->servicer_name.'%')
               ->whereIn('servicer_id',$servicer_id)
               ->get();



           $order_product_id = [];
           $driver_info = [];
           $order_id = [];
           $servicer_name = [];
           foreach ($order as $key=>$value){
              $order_id[] = $value->id;
              $order_product_id[] = explode(',',$value->order_product_id);
              $arr = explode(',',$value->order_product_id);
              if(!in_array($value->servicer_name,$servicer_name)){
                  $servicer_name[] = $value->servicer_name;
              }
              foreach ($arr as $k=>$v){
               $driver_info[$v]['order_id'] = $value->id;
               $driver_info[$v]['servicer_name'] = $value->servicer_name;
               $driver_info[$v]['driver_id'] = $value->driver_id;
               $driver_info[$v]['driver_name'] = $value->driver_name;
               $driver_info[$v]['driver_get_date'] = $value->driver_get_date;
               $driver_info[$v]['arriver_date'] = $value->arriver_date;
               $driver_info[$v]['supplyer_company'] = $value->supplyer_company;
               $driver_info[$v]['total_sell'] = $value->total_sell;
               $driver_info[$v]['already_pay'] = $value->already_pay;
              }
           }
           $arr_id = [];
           foreach ($order_product_id as $key=>$value){
             $arr_id = array_merge($arr_id,$value);
           }

            //把提成所有情况计算出来
             $servicer_percentage = DB::table('order_servicer_percentage')
             ->whereIn('order_id',$order_id)
             ->get();
            $servicer_percentage = json_decode($servicer_percentage,1);
            $servicer_percentage_arr = [];
            foreach ($servicer_percentage as $key=>$value){
                if(!isset($servicer_percentage_arr[$value['order_id']])){
                    if($value['type'] == 0) {
                        $servicer_percentage_arr[$value['order_id']]['master_percentage_money'] = $value['percentage_money'];
                    }else if($value['type'] == 1){
                        $servicer_percentage_arr[$value['order_id']]['brance_percentage_money'] = $value['percentage_money'];
                    }
                }else{
                    if($value['type'] == 0) {
                        if(isset($servicer_percentage_arr[$value['order_id']]['master_percentage_money'])) {
                            $servicer_percentage_arr[$value['order_id']]['master_percentage_money'] += $value['percentage_money'];
                        }else{
                            $servicer_percentage_arr[$value['order_id']]['master_percentage_money'] = $value['percentage_money'];
                        }
                    }else if($value['type'] == 1){
                        if(isset($servicer_percentage_arr[$value['order_id']]['brance_percentage_money'])) {
                            $servicer_percentage_arr[$value['order_id']]['brance_percentage_money'] += $value['percentage_money'];
                        }else{
                            $servicer_percentage_arr[$value['order_id']]['brance_percentage_money'] = $value['percentage_money'];
                        }
                    }
                }
            }

           $order_product = DB::table('order_product')
               ->select('*')
               ->whereIn('id',$arr_id)->get();

           $order_product = json_decode($order_product,1);
           foreach ($order_product as $key=>$value){
              $order_product[$key]['order_id'] = $driver_info[$value['id']]['order_id'];
              $order_product[$key]['driver_id'] = $driver_info[$value['id']]['driver_id'];
              $order_product[$key]['driver_name'] = $driver_info[$value['id']]['driver_name'];
              $order_product[$key]['driver_get_date'] = $driver_info[$value['id']]['driver_get_date'];
              $order_product[$key]['arriver_date'] = $driver_info[$value['id']]['arriver_date'];
              $order_product[$key]['supplyer_company'] = $driver_info[$value['id']]['supplyer_company'];
              $order_product[$key]['total_sell'] = $driver_info[$value['id']]['total_sell'];
              $order_product[$key]['already_pay'] = $driver_info[$value['id']]['already_pay'];
              $order_product[$key]['servicer_name'] = $driver_info[$value['id']]['servicer_name'];
              if(isset($servicer_percentage_arr[$driver_info[$value['id']]['order_id']])) {
                  $order_product[$key]['master_percentage_money'] = $servicer_percentage_arr[$driver_info[$value['id']]['order_id']]['master_percentage_money'];
                  $order_product[$key]['brance_percentage_money'] = $servicer_percentage_arr[$driver_info[$value['id']]['order_id']]['brance_percentage_money'];
              }else{
                  $order_product[$key]['master_percentage_money'] = 0;
                  $order_product[$key]['brance_percentage_money'] = 0;
              }
           }
            $order_product  = (object)$order_product;
           return view('admin/finance/performan_check',['product'=>$order_product,'servicer_name'=>$servicer_name]);
      }


          /*
           * 基础表
           * */
        public function basic_table(Request $request){
            $order = DB::table('order')
                ->where('status',1)
                ->where('code',3)
                ->paginate(50);

            $items = $order->items();
            $order_id = [];
            foreach ($items as $key=>$value){
                $order_id[] = $value->id;
            }
            $table = [];
            foreach ($items as $key=>$value){
                $table[] = (array)$value;
            }

            $supplement_order = DB::table('supplement_order')->select('*')->whereIn('order_id',$order_id)->get();
            $supplement = [];
            if(!empty($supplement_order)) {
                foreach ($supplement_order as $key => $value) {
                    $supplement[$value->order_id]['supplement_factory_date'] = $value->supplement_factory_date;
                    $supplement[$value->order_id]['supplement_factory_id'] = $value->supplement_factory_id;
                    $supplement[$value->order_id]['supplement_store'] = $value->supplement_store;
                    $supplement[$value->order_id]['supplement_tax'] = $value->supplement_tax;
                    $supplement[$value->order_id]['supplement_factory_receipt'] = $value->supplement_factory_receipt;
                    $supplement[$value->order_id]['supplement_customer_receipt'] = $value->supplement_customer_receipt;
                    $supplement[$value->order_id]['supplement_customer_loan'] = $value->supplement_customer_loan;
                    $supplement[$value->order_id]['supplement_freight_cost'] = $value->supplement_freight_cost;
                    $supplement[$value->order_id]['supplement_son_customer'] = $value->supplement_son_customer;
                    $supplement[$value->order_id]['supplement_readd_price'] = $value->supplement_readd_price;
                    $supplement[$value->order_id]['supplement_get_totoal'] = $value->supplement_get_totoal;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_sell'] = $value->supplement_total_sell;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_profit'] = $value->supplement_total_profit;
                    $supplement[$value->order_id]['supplement_unit_profit'] = $value->supplement_unit_profit;
                    $supplement[$value->order_id]['supplement_arrive_status'] = $value->supplement_arrive_status;
                    $supplement[$value->order_id]['supplement_unit_price'] = $value->supplement_unit_price;

                }
            }

            foreach ($table as $key=>$value){
                if(isset($supplement[$value['id']])){
                      $table[$key]['supplement_factory_date']   = $supplement[$value['id']]['supplement_factory_date'];
                      $table[$key]['supplement_factory_id']   = $supplement[$value['id']]['supplement_factory_id'];
                      $table[$key]['supplement_store']   = $supplement[$value['id']]['supplement_store'];
                      $table[$key]['supplement_tax']   = $supplement[$value['id']]['supplement_tax'];
                      $table[$key]['supplement_factory_receipt']   = $supplement[$value['id']]['supplement_factory_receipt'];
                      $table[$key]['supplement_customer_receipt']   = $supplement[$value['id']]['supplement_customer_receipt'];
                      $table[$key]['supplement_customer_loan']   = $supplement[$value['id']]['supplement_customer_loan'];
                      $table[$key]['supplement_freight_cost']   = $supplement[$value['id']]['supplement_freight_cost'];
                      $table[$key]['supplement_son_customer']   = $supplement[$value['id']]['supplement_son_customer'];
                       $table[$key]['supplement_readd_price']   = $supplement[$value['id']]['supplement_readd_price'];
                      $table[$key]['supplement_get_totoal']    = $supplement[$value['id']]['supplement_get_totoal'];
                      $table[$key]['supplement_price']   = $supplement[$value['id']]['supplement_price'];
                      $table[$key]['supplement_total_sell']   = $supplement[$value['id']]['supplement_total_sell'];
                      $table[$key]['supplement_total_profit']  = $supplement[$value['id']]['supplement_total_profit'];
                      $table[$key]['supplement_unit_profit']    = $supplement[$value['id']]['supplement_unit_profit'];
                      $table[$key]['supplement_arrive_status']    = $supplement[$value['id']]['supplement_arrive_status'];
                      $table[$key]['supplement_unit_price']    = $supplement[$value['id']]['supplement_unit_price'];
                }else{
                      $table[$key]['supplement_factory_date']   = '';
                      $table[$key]['supplement_factory_id']   = '';
                      $table[$key]['supplement_unit_price']   = '';
                      $table[$key]['supplement_store']   = '';
                      $table[$key]['supplement_tax']   = '否';
                      $table[$key]['supplement_factory_receipt']   = '';
                      $table[$key]['supplement_customer_receipt']   = '';
                      $table[$key]['supplement_customer_loan']   = '';
                      $table[$key]['supplement_freight_cost']   = '';
                      $table[$key]['supplement_son_customer']   = '';
                      $table[$key]['supplement_readd_price']   = '';
                      $table[$key]['supplement_get_totoal']   = '';
                      $table[$key]['supplement_price']   = '';
                      $table[$key]['supplement_total_sell']   = '';
                      $table[$key]['supplement_total_profit']   = '';
                      $table[$key]['supplement_unit_profit']   = '';
                      $table[$key]['supplement_arrive_status']   = '是';

                }
            }

             return view('admin/finance/basic_table',['order'=>$order,'table'=>$table]);
        }


       /*
       * 基础表的修改
       * */
      public function basic_table_edite(Request $request){
            $name = $request->name;
            //var_dump(substr($name,0,10));
            if(substr($name,0,10)=='supplement'){
                  //进行补充操作
                 $supplement_order = DB::table('supplement_order')->where('order_id',$request->id)->first();
                 if(!empty($supplement_order)){
                     DB::table('supplement_order')->where('order_id',$request->id)->update(array($name=>$request->value));
                     return 1;
                 }else{
                     var_dump($supplement_order);
                     DB::table('supplement_order')->insert(array('order_id'=>$request->id,$name=>$request->value));
                     return 2;
                 }
            }else{
                  //修改
                  DB::table('order')->where('id',$request->id)->update(array($name=>$request->value));
                  return 3;
            }
      }


      //基础表下载
      public function basic_table_download(Request $request){
            $order = DB::table('order')
                ->where('status',1)
                ->where('code',3)
                ->paginate(50);

            $items = $order->items();
            $order_id = [];
            foreach ($items as $key=>$value){
                $order_id[] = $value->id;
            }
            $table = [];
            foreach ($items as $key=>$value){
                $table[] = (array)$value;
            }
            $supplement_order = DB::table('supplement_order')->select('*')->whereIn('order_id',$order_id)->get();
            $supplement = [];
            if(!empty($supplement_order)) {
                foreach ($supplement_order as $key => $value) {
                    $supplement[$value->order_id]['supplement_factory_date'] = $value->supplement_factory_date;
                    $supplement[$value->order_id]['supplement_factory_id'] = $value->supplement_factory_id;
                    $supplement[$value->order_id]['supplement_store'] = $value->supplement_store;
                    $supplement[$value->order_id]['supplement_tax'] = $value->supplement_tax;
                    $supplement[$value->order_id]['supplement_factory_receipt'] = $value->supplement_factory_receipt;
                    $supplement[$value->order_id]['supplement_customer_receipt'] = $value->supplement_customer_receipt;
                    $supplement[$value->order_id]['supplement_customer_loan'] = $value->supplement_customer_loan;
                    $supplement[$value->order_id]['supplement_freight_cost'] = $value->supplement_freight_cost;
                    $supplement[$value->order_id]['supplement_son_customer'] = $value->supplement_son_customer;
                    $supplement[$value->order_id]['supplement_readd_price'] = $value->supplement_readd_price;
                    $supplement[$value->order_id]['supplement_get_totoal'] = $value->supplement_get_totoal;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_sell'] = $value->supplement_total_sell;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_profit'] = $value->supplement_total_profit;
                    $supplement[$value->order_id]['supplement_unit_profit'] = $value->supplement_unit_profit;
                    $supplement[$value->order_id]['supplement_arrive_status'] = $value->supplement_arrive_status;
                    $supplement[$value->order_id]['supplement_unit_price'] = $value->supplement_unit_price;

                }
            }

            foreach ($table as $key=>$value){
                if(isset($supplement[$value['id']])){
                      $table[$key]['supplement_factory_date']   = $supplement[$value['id']]['supplement_factory_date'];
                      $table[$key]['supplement_factory_id']   = $supplement[$value['id']]['supplement_factory_id'];
                      $table[$key]['supplement_store']   = $supplement[$value['id']]['supplement_store'];
                      $table[$key]['supplement_tax']   = $supplement[$value['id']]['supplement_tax'];
                      $table[$key]['supplement_factory_receipt']   = $supplement[$value['id']]['supplement_factory_receipt'];
                      $table[$key]['supplement_customer_receipt']   = $supplement[$value['id']]['supplement_customer_receipt'];
                      $table[$key]['supplement_customer_loan']   = $supplement[$value['id']]['supplement_customer_loan'];
                      $table[$key]['supplement_freight_cost']   = $supplement[$value['id']]['supplement_freight_cost'];
                      $table[$key]['supplement_son_customer']   = $supplement[$value['id']]['supplement_son_customer'];
                       $table[$key]['supplement_readd_price']   = $supplement[$value['id']]['supplement_readd_price'];
                      $table[$key]['supplement_get_totoal']    = $supplement[$value['id']]['supplement_get_totoal'];
                      $table[$key]['supplement_price']   = $supplement[$value['id']]['supplement_price'];
                      $table[$key]['supplement_total_sell']   = $supplement[$value['id']]['supplement_total_sell'];
                      $table[$key]['supplement_total_profit']  = $supplement[$value['id']]['supplement_total_profit'];
                      $table[$key]['supplement_unit_profit']    = $supplement[$value['id']]['supplement_unit_profit'];
                      $table[$key]['supplement_arrive_status']    = $supplement[$value['id']]['supplement_arrive_status'];
                      $table[$key]['supplement_unit_price']    = $supplement[$value['id']]['supplement_unit_price'];
                }else{
                      $table[$key]['supplement_factory_date']   = '';
                      $table[$key]['supplement_factory_id']   = '';
                      $table[$key]['supplement_unit_price']   = '';
                      $table[$key]['supplement_store']   = '';
                      $table[$key]['supplement_tax']   = '否';
                      $table[$key]['supplement_factory_receipt']   = '';
                      $table[$key]['supplement_customer_receipt']   = '';
                      $table[$key]['supplement_customer_loan']   = '';
                      $table[$key]['supplement_freight_cost']   = '';
                      $table[$key]['supplement_son_customer']   = '';
                      $table[$key]['supplement_readd_price']   = '';
                      $table[$key]['supplement_get_totoal']   = '';
                      $table[$key]['supplement_price']   = '';
                      $table[$key]['supplement_total_sell']   = '';
                      $table[$key]['supplement_total_profit']   = '';
                      $table[$key]['supplement_unit_profit']   = '';
                      $table[$key]['supplement_arrive_status']   = '是';

                }
            }

            return view('admin/finance/basic_table_download',['order'=>$order,'table'=>$table]);
      }



      /*
       * 应收表
       * */
        public function get_table(Request $request){
            if(Session::get('role_id') == 1 ) {
                  $servicer = DB::table('servicer')
                       ->select('id')
                        ->get();
            }else if(Session::get('role_id') == 2 ){
                  $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('session_id'))
                       ->get();
             }
              $servicer_id = [];
              foreach ($servicer as $key=>$value){
                  $servicer_id[] = $value->id;
              }

               $supplyer = DB::table('customer')
                       ->select('id','customer_name as name')
                       ->where('status',0)
                       ->whereIn('servicer_id',$servicer_id)->get();

              $supplyer_id = [];
              foreach ($supplyer as $key=>$value){
                     $supplyer_id[] = $value->id;
              }


              if(!isset($request->start_date)) {
                  $start_date = date('Y-m', time());
                  $arr = explode('-',$start_date);
                  $start_date = $arr[0].'-'.$arr[1].'-1 00:00:00';
                  $end = $arr[1] +1;
                  $end_date =  $arr[0].'-'.$end.'-1 00:00:00';
              }else{
                  $arr = explode('-',$request->start_date);
                  $start_date = $arr[0].'-'.$arr[1].'-1 00:00:00';
                  $end = $arr[1] +1;
                  $end_date =  $arr[0].'-'.$end.'-1 00:00:00';
              }

               $table_date = array('year'=>$arr[0],'month'=>$arr[1],'end'=>$end);
               $order_get = DB::table('order_get')
                   ->where('date',$start_date)
                   ->whereIn('customer_id',$supplyer_id)
                   ->get();

               $order_table = [];
               if(!empty($order_get)){
                   foreach ($order_get as $key=>$value){
                        $order_table[$value->customer_id] = (array)$value;
                   }

               }
               $supplyer = json_decode($supplyer,1);
               foreach ($supplyer as $key=>$value){
                        if(isset($order_table[$value['id']])){
                              $supplyer[$key]['order_origin_num'] = $order_table[$value['id']]['order_origin_num'];
                              $supplyer[$key]['order_should_get'] = $order_table[$value['id']]['order_should_get'];
                              $supplyer[$key]['order_already_get'] = $order_table[$value['id']]['order_already_get'];
                              $supplyer[$key]['order_month_num'] = $order_table[$value['id']]['order_month_num'];
                              $supplyer[$key]['order_belong_servicer'] = $order_table[$value['id']]['order_belong_servicer'];
                              $supplyer[$key]['pay_method'] = $order_table[$value['id']]['pay_method'];
                              $supplyer[$key]['mark'] = $order_table[$value['id']]['mark'];
                              $supplyer[$key]['order_date_remain'] = $order_table[$value['id']]['order_date_remain'];
                              $supplyer[$key]['date'] = $order_table[$value['id']]['date'];
                        }else{
                              $supplyer[$key]['order_origin_num'] = 0;
                              $supplyer[$key]['order_should_get'] = 0;
                              $supplyer[$key]['order_already_get'] = 0;
                              $supplyer[$key]['order_month_num'] = 0;
                              $supplyer[$key]['order_belong_servicer'] = '';
                              $supplyer[$key]['pay_method'] = '';
                              $supplyer[$key]['mark'] = '';
                              $supplyer[$key]['order_date_remain'] = 0;
                              $supplyer[$key]['date'] = $start_date;
                        }
               }


            return view('admin/finance/get_table',['table_date'=>$table_date,'supplyer'=>$supplyer,'end_date'=>$end_date]);

        }


        /*
         *应收表编辑
         * */
        public function get_table_edite(Request $request){
                 $all = $request->all();
                 $name = $request->name;
                  //进行补充操作
                 $order_get = DB::table('order_get')
                     ->where('customer_id',$request->id)
                     ->where('date',$request->date)
                     ->first();

                 if(!empty($order_get)){
                     DB::table('order_get')->where('customer_id',$request->id)->update(array($name=>$request->value));
                     return 1;
                 }else{
                     var_dump($order_get);
                     DB::table('order_get')->insert(array('customer_id'=>$request->id,$name=>$request->value,'date'=>$request->date));
                     return 2;
                 }

        }


        /*
         * 应收总表下载
         * */
        public function get_table_download(Request $request)
        {
            if(Session::get('role_id') == 1 ) {
                $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('session_id'))
                        ->get();
            }else if(Session::get('role_id') == 2 ){
                  $servicer = DB::table('servicer')
                       ->select('id')
                       ->where('company_id',Session::get('session_id'))
                        ->get();
             }
              $servicer_id = [];
              foreach ($servicer as $key=>$value){
                  $servicer_id[] = $value->id;
              }

               $supplyer = DB::table('customer')
                       ->select('id','customer_name as name')
                       ->where('status',0)
                       ->whereIn('servicer_id',$servicer_id)->get();

              $supplyer_id = [];
              foreach ($supplyer as $key=>$value){
                     $supplyer_id[] = $value->id;
              }


              if(!isset($request->start_date)) {
                  $start_date = date('Y-m', time());
                  $arr = explode('-',$start_date);
                  $start_date = $arr[0].'-'.$arr[1].'-1 00:00:00';
                  $end = $arr[1] +1;
                  $end_date =  $arr[0].'-'.$end.'-1 00:00:00';
              }else{
                  $arr = explode('-',$request->start_date);
                  $start_date = $arr[0].'-'.$arr[1].'-1 00:00:00';
                  $end = $arr[1] +1;
                  $end_date =  $arr[0].'-'.$end.'-1 00:00:00';
              }

               $table_date = array('year'=>$arr[0],'month'=>$arr[1],'end'=>$end);
               $order_get = DB::table('order_get')
                   ->where('date',$start_date)
                   ->whereIn('customer_id',$supplyer_id)
                   ->get();

               $order_table = [];
               if(!empty($order_get)){
                   foreach ($order_get as $key=>$value){
                        $order_table[$value->customer_id] = (array)$value;
                   }
               }
               $supplyer = json_decode($supplyer,1);
               foreach ($supplyer as $key=>$value){
                        if(isset($order_table[$value['id']])){
                              $supplyer[$key]['order_origin_num'] = $order_table[$value['id']]['order_origin_num'];
                              $supplyer[$key]['order_should_get'] = $order_table[$value['id']]['order_should_get'];
                              $supplyer[$key]['order_already_get'] = $order_table[$value['id']]['order_already_get'];
                              $supplyer[$key]['order_month_num'] = $order_table[$value['id']]['order_month_num'];
                              $supplyer[$key]['order_belong_servicer'] = $order_table[$value['id']]['order_belong_servicer'];
                              $supplyer[$key]['pay_method'] = $order_table[$value['id']]['pay_method'];
                              $supplyer[$key]['mark'] = $order_table[$value['id']]['mark'];
                              $supplyer[$key]['order_date_remain'] = $order_table[$value['id']]['order_date_remain'];
                              $supplyer[$key]['date'] = $order_table[$value['id']]['date'];
                        }else{
                              $supplyer[$key]['order_origin_num'] = 0;
                              $supplyer[$key]['order_should_get'] = 0;
                              $supplyer[$key]['order_already_get'] = 0;
                              $supplyer[$key]['order_month_num'] = 0;
                              $supplyer[$key]['order_belong_servicer'] = '';
                              $supplyer[$key]['pay_method'] = '';
                              $supplyer[$key]['mark'] = '';
                              $supplyer[$key]['order_date_remain'] = 0;
                              $supplyer[$key]['date'] = $start_date;
                        }
               }


            return view('admin/finance/get_table_download',['table_date'=>$table_date,'supplyer'=>$supplyer]);
        }




        /*
         * 应付表
         * */
        public function pay_table(){
                if(Session::get('role_id') == 1 ) {
                $supplyer = DB::table('supplyer_company')
                       ->select('id','name')
                        ->get();
            }else if(Session::get('role_id') == 2 ){
                $supplyer = DB::table('supplyer_company')
                       ->select('id','name')
                       ->where('company_id',Session::get('session_id'))
                        ->get();
             }

              $supplyer_id = [];
              foreach ($supplyer as $key=>$value){
                     $supplyer_id[] = $value->id;
              }


              if(!isset($request->start_date)) {
                  $start_date = date('Y-m', time());
                  $arr = explode('-',$start_date);
                  $start_date = $arr[0].'-'.$arr[1].'-1 00:00:00';
                  $end = $arr[1] +1;
                  $end_date =  $arr[0].'-'.$end.'-1 00:00:00';
              }else{
                  $arr = explode('-',$request->start_date);
                  $start_date = $arr[0].'-'.$arr[1].'-1 00:00:00';
                  $end = $arr[1] +1;
                  $end_date =  $arr[0].'-'.$end.'-1 00:00:00';
              }

               $table_date = array('year'=>$arr[0],'month'=>$arr[1],'end'=>$end);
               $order_pay = DB::table('order_pay')
                   ->where('date',$start_date)
                   ->whereIn('supplyer_id',$supplyer_id)
                   ->get();

               $order_table = [];
               if(!empty($order_pay)){
                   foreach ($order_pay as $key=>$value){
                        $order_table[$value->supplyer_id] = (array)$value;
                   }
               }
               $supplyer = json_decode($supplyer,1);
               foreach ($supplyer as $key=>$value){
                        if(isset($order_table[$value['id']])){
                              $supplyer[$key]['order_origin_num'] = $order_table[$value['id']]['order_origin_num'];
                              $supplyer[$key]['card_include'] = $order_table[$value['id']]['card_include'];
                              $supplyer[$key]['order_should_get'] = $order_table[$value['id']]['order_should_get'];
                              $supplyer[$key]['order_already_get'] = $order_table[$value['id']]['order_already_get'];
                              $supplyer[$key]['order_month_num'] = $order_table[$value['id']]['order_month_num'];
                              $supplyer[$key]['order_belong_servicer'] = $order_table[$value['id']]['order_belong_servicer'];
                              $supplyer[$key]['pay_method'] = $order_table[$value['id']]['pay_method'];
                              $supplyer[$key]['mark'] = $order_table[$value['id']]['mark'];
                              $supplyer[$key]['order_date_remain'] = $order_table[$value['id']]['order_date_remain'];
                              $supplyer[$key]['date'] = $order_table[$value['id']]['date'];
                        }else{
                              $supplyer[$key]['card_include'] = '含票';
                              $supplyer[$key]['order_origin_num'] = 0;
                              $supplyer[$key]['order_should_get'] = 0;
                              $supplyer[$key]['order_already_get'] = 0;
                              $supplyer[$key]['order_month_num'] = 0;
                              $supplyer[$key]['order_belong_servicer'] = '';
                              $supplyer[$key]['pay_method'] = '';
                              $supplyer[$key]['mark'] = '';
                              $supplyer[$key]['order_date_remain'] = 0;
                              $supplyer[$key]['date'] = $start_date;
                        }
               }
            return view('admin/finance/pay_table',['table_date'=>$table_date,'supplyer'=>$supplyer,'end_date'=>$end_date]);
        }


        /*
         *应收表编辑
         * */
        public function pay_table_edite(Request $request){
                 $all = $request->all();
                 $name = $request->name;
                  //进行补充操作
                 $order_pay = DB::table('order_pay')
                     ->where('supplyer_id',$request->id)
                     ->where('date',$request->date)
                     ->first();
                 if(!empty($order_pay)){
                     DB::table('order_pay')->where('supplyer_id',$request->id)->update(array($name=>$request->value));
                     return 1;
                 }else{

                     DB::table('order_pay')->insert(array('supplyer_id'=>$request->id,$name=>$request->value,'date'=>$request->date));
                     return 2;
                 }

        }

        /*
         * 车运价目表
         * */
        public function driver_table(){

            return view('admin/finance/driver_table');

        }

         /*
         * 处理报表详情
         * */
        public function get_table_detail(Request $request){

            $customer = DB::table('customer')
                ->select('*')
                ->where('id',$request->customer_id)
                ->first();

            $order = DB::table('order')
                ->select(
                    'id','driver_get_date','order_code','agreement_code',
                    'product_name','standards','pack_method','product_num','unit_price',
                    'total_sell','get_address','servicer_mark'
                    )
                ->where('make_date','>',$request->date)
                ->where('make_date','<',$request->end_date)
                ->where('customer_id',$request->customer_id)
                ->get();
            $order = json_decode($order,1);

            if(!empty($order)) {
                foreach ($order as $key=>$value) {
                    $order_accounts_pay = DB::table('order_accounts_pay')
                        ->select('pay_date', 'pay_money', 'pay_mark')
                        ->where('order_id', $value['id'])
                        ->first();
                    if (!empty($order_accounts_pay)) {
                        $order[$key]['pay_date'] = $order_accounts_pay->pay_date;
                        $order[$key]['pay_money'] = $order_accounts_pay->pay_money;
                        $order[$key]['pay_mark'] = $order_accounts_pay->pay_mark;
                    } else {
                        $order[$key]['pay_date'] = '';
                        $order[$key]['pay_money'] = '';
                        $order[$key]['pay_mark'] = '';
                    }
                }
            }

            $order_accounts = DB::table('order_accounts')
                ->where('customer_id',$request->customer_id)
                ->where('date',$request->date)
                ->first();
            $order_accounts = (array)$order_accounts;
            if(empty($order_accounts)){
                $order_info = DB::select('SHOW COLUMNS FROM re_order_accounts');
                foreach ($order_info as $key=>$value){
                       $value = (array)$value;
                        if(empty($order_accounts[$value['Field']])){
                            $order_accounts[$value['Field']] = '';
                        }
                }
            }
                        if(empty($order_accounts['customer_name'])){
                            $order_accounts['customer_name'] = $customer->customer_name;
                        }
                        if(empty($order_accounts['customer_id'])){
                            $order_accounts['customer_id'] = $customer->id;
                        }
                        if(empty($order_accounts['customer_register_address'])){
                            $order_accounts['customer_register_address'] = $customer->address;
                        }
                        if(empty($order_accounts['customer_contact'])){
                            $order_accounts['customer_contact'] = $customer->contact;
                        }
                        if(empty($order_accounts['customer_phone'])){
                            $order_accounts['customer_phone'] = $customer->telephone;
                        }
                        if(empty($order_accounts['customer_send'])){
                            $order_accounts['customer_send'] = $customer->contact_telephone;
                        }

                return view('admin/finance/get_table_detail',[
                    'order_accounts'=>$order_accounts,
                    'order'=>$order,
                    'start_date'=>$request->date,
                    'end_date'=>$request->end_date
                ]);
        }

        /*
         * 处理应付表
         * */
        public function pay_table_detail_download(Request $request){

             $order = DB::table('order')
                ->where('status',1)
                ->where('code',3)
                ->where('make_date','>',$request->date)
                ->where('make_date','<',$request->end_date)
                ->where('supplyer_company',$request->supplyer_name)
                ->paginate(500);

            $items = $order->items();
            $order_id = [];
            foreach ($items as $key=>$value){
                $order_id[] = $value->id;
            }
            $table = [];
            foreach ($items as $key=>$value){
                $table[] = (array)$value;
            }

            $supplement_order = DB::table('supplement_order')->select('*')->whereIn('order_id',$order_id)->get();
            $order_accounts_pay = DB::table('order_accounts_pay')->select('*')->whereIn('order_id',$order_id)->get();
            $supplement = [];
            $order_pay = [];
            if(!empty($supplement_order)) {
                foreach ($supplement_order as $key => $value) {
                    $supplement[$value->order_id]['supplement_factory_date'] = $value->supplement_factory_date;
                    $supplement[$value->order_id]['supplement_factory_id'] = $value->supplement_factory_id;
                    $supplement[$value->order_id]['supplement_store'] = $value->supplement_store;
                    $supplement[$value->order_id]['supplement_tax'] = $value->supplement_tax;
                    $supplement[$value->order_id]['supplement_factory_receipt'] = $value->supplement_factory_receipt;
                    $supplement[$value->order_id]['supplement_customer_receipt'] = $value->supplement_customer_receipt;
                    $supplement[$value->order_id]['supplement_customer_loan'] = $value->supplement_customer_loan;
                    $supplement[$value->order_id]['supplement_freight_cost'] = $value->supplement_freight_cost;
                    $supplement[$value->order_id]['supplement_son_customer'] = $value->supplement_son_customer;
                    $supplement[$value->order_id]['supplement_readd_price'] = $value->supplement_readd_price;
                    $supplement[$value->order_id]['supplement_get_totoal'] = $value->supplement_get_totoal;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_sell'] = $value->supplement_total_sell;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_profit'] = $value->supplement_total_profit;
                    $supplement[$value->order_id]['supplement_unit_profit'] = $value->supplement_unit_profit;
                    $supplement[$value->order_id]['supplement_arrive_status'] = $value->supplement_arrive_status;
                    $supplement[$value->order_id]['supplement_unit_price'] = $value->supplement_unit_price;

                }
            }

            if(!empty($order_accounts_pay)) {
                foreach ($order_accounts_pay as $key => $value) {
                     $order_pay[$value->order_id] = (array)$value;
                }
            }


            foreach ($table as $key=>$value){
                if(isset($supplement[$value['id']])){
                      $table[$key]['supplement_factory_date']   = $supplement[$value['id']]['supplement_factory_date'];
                      $table[$key]['supplement_factory_id']   = $supplement[$value['id']]['supplement_factory_id'];
                      $table[$key]['supplement_store']   = $supplement[$value['id']]['supplement_store'];
                      $table[$key]['supplement_tax']   = $supplement[$value['id']]['supplement_tax'];
                      $table[$key]['supplement_factory_receipt']   = $supplement[$value['id']]['supplement_factory_receipt'];
                      $table[$key]['supplement_customer_receipt']   = $supplement[$value['id']]['supplement_customer_receipt'];
                      $table[$key]['supplement_customer_loan']   = $supplement[$value['id']]['supplement_customer_loan'];
                      $table[$key]['supplement_freight_cost']   = $supplement[$value['id']]['supplement_freight_cost'];
                      $table[$key]['supplement_son_customer']   = $supplement[$value['id']]['supplement_son_customer'];
                       $table[$key]['supplement_readd_price']   = $supplement[$value['id']]['supplement_readd_price'];
                      $table[$key]['supplement_get_totoal']    = $supplement[$value['id']]['supplement_get_totoal'];
                      $table[$key]['supplement_price']   = $supplement[$value['id']]['supplement_price'];
                      $table[$key]['supplement_total_sell']   = $supplement[$value['id']]['supplement_total_sell'];
                      $table[$key]['supplement_total_profit']  = $supplement[$value['id']]['supplement_total_profit'];
                      $table[$key]['supplement_unit_profit']    = $supplement[$value['id']]['supplement_unit_profit'];
                      $table[$key]['supplement_arrive_status']    = $supplement[$value['id']]['supplement_arrive_status'];
                      $table[$key]['supplement_unit_price']    = $supplement[$value['id']]['supplement_unit_price'];
                }else{
                      $table[$key]['supplement_factory_date']   = '';
                      $table[$key]['supplement_factory_id']   = '';
                      $table[$key]['supplement_unit_price']   = '';
                      $table[$key]['supplement_store']   = '';
                      $table[$key]['supplement_tax']   = '否';
                      $table[$key]['supplement_factory_receipt']   = '';
                      $table[$key]['supplement_customer_receipt']   = '';
                      $table[$key]['supplement_customer_loan']   = '';
                      $table[$key]['supplement_freight_cost']   = '';
                      $table[$key]['supplement_son_customer']   = '';
                      $table[$key]['supplement_readd_price']   = '';
                      $table[$key]['supplement_get_totoal']   = '';
                      $table[$key]['supplement_price']   = '';
                      $table[$key]['supplement_total_sell']   = '';
                      $table[$key]['supplement_total_profit']   = '';
                      $table[$key]['supplement_unit_profit']   = '';
                      $table[$key]['supplement_arrive_status']   = '是';

                }

                if(isset($order_pay[$value['id']])){
                      $table[$key]['pay_date']   = $order_pay[$value['id']]['pay_date'];
                      $table[$key]['pay_money']   = $order_pay[$value['id']]['pay_money'];
                      $table[$key]['pay_mark']   = $order_pay[$value['id']]['pay_mark'];
                }else{
                      $table[$key]['pay_date']   = '';
                      $table[$key]['pay_money']   = '';
                      $table[$key]['pay_mark']   = '';
                }
            }

            $order_accounts_countadd =  DB::table('order_accounts_countadd')
                ->where('date',$request->date)
                ->where('supplyer_name',$request->supplyer_name)->first();
            $order_accounts_countadd = (array)$order_accounts_countadd;
            if(empty($order_accounts_countadd)){
                $order_accounts_countadd = DB::select('SHOW COLUMNS FROM re_order_accounts_countadd');
                foreach ($order_accounts_countadd as $key=>$value){
                       $value = (array)$value;
                        if(empty($order_accounts_countadd[$value['Field']])){
                            $order_accounts_countadd[$value['Field']] = '';
                        }
                }
            }

            return view('admin/finance/pay_table_detail_download',[
                    'order_accounts_countadd'=>$order_accounts_countadd,
                    'order'=>$table,
                    'supplyer_name'=>$request->supplyer_name,
                    'start_date'=>$request->date,
                    'end_date'=>$request->end_date
                ]);


        }


        /*
         * 处理应付表
         * */
        public function pay_table_detail(Request $request){

             $order = DB::table('order')
                ->where('status',1)
                ->where('code',3)
                ->where('make_date','>',$request->date)
                ->where('make_date','<',$request->end_date)
                ->where('supplyer_company',$request->supplyer_name)
                ->paginate(500);

            $items = $order->items();
            $order_id = [];
            foreach ($items as $key=>$value){
                $order_id[] = $value->id;
            }
            $table = [];
            foreach ($items as $key=>$value){
                $table[] = (array)$value;
            }

            $supplement_order = DB::table('supplement_order')->select('*')->whereIn('order_id',$order_id)->get();
            $order_accounts_pay = DB::table('order_accounts_pay')->select('*')->whereIn('order_id',$order_id)->get();
            $supplement = [];
            $order_pay = [];
            if(!empty($supplement_order)) {
                foreach ($supplement_order as $key => $value) {
                    $supplement[$value->order_id]['supplement_factory_date'] = $value->supplement_factory_date;
                    $supplement[$value->order_id]['supplement_factory_id'] = $value->supplement_factory_id;
                    $supplement[$value->order_id]['supplement_store'] = $value->supplement_store;
                    $supplement[$value->order_id]['supplement_tax'] = $value->supplement_tax;
                    $supplement[$value->order_id]['supplement_factory_receipt'] = $value->supplement_factory_receipt;
                    $supplement[$value->order_id]['supplement_customer_receipt'] = $value->supplement_customer_receipt;
                    $supplement[$value->order_id]['supplement_customer_loan'] = $value->supplement_customer_loan;
                    $supplement[$value->order_id]['supplement_freight_cost'] = $value->supplement_freight_cost;
                    $supplement[$value->order_id]['supplement_son_customer'] = $value->supplement_son_customer;
                    $supplement[$value->order_id]['supplement_readd_price'] = $value->supplement_readd_price;
                    $supplement[$value->order_id]['supplement_get_totoal'] = $value->supplement_get_totoal;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_sell'] = $value->supplement_total_sell;
                    $supplement[$value->order_id]['supplement_price'] = $value->supplement_price;
                    $supplement[$value->order_id]['supplement_total_profit'] = $value->supplement_total_profit;
                    $supplement[$value->order_id]['supplement_unit_profit'] = $value->supplement_unit_profit;
                    $supplement[$value->order_id]['supplement_arrive_status'] = $value->supplement_arrive_status;
                    $supplement[$value->order_id]['supplement_unit_price'] = $value->supplement_unit_price;

                }
            }

            if(!empty($order_accounts_pay)) {
                foreach ($order_accounts_pay as $key => $value) {
                     $order_pay[$value->order_id] = (array)$value;
                }
            }


            foreach ($table as $key=>$value){
                if(isset($supplement[$value['id']])){
                      $table[$key]['supplement_factory_date']   = $supplement[$value['id']]['supplement_factory_date'];
                      $table[$key]['supplement_factory_id']   = $supplement[$value['id']]['supplement_factory_id'];
                      $table[$key]['supplement_store']   = $supplement[$value['id']]['supplement_store'];
                      $table[$key]['supplement_tax']   = $supplement[$value['id']]['supplement_tax'];
                      $table[$key]['supplement_factory_receipt']   = $supplement[$value['id']]['supplement_factory_receipt'];
                      $table[$key]['supplement_customer_receipt']   = $supplement[$value['id']]['supplement_customer_receipt'];
                      $table[$key]['supplement_customer_loan']   = $supplement[$value['id']]['supplement_customer_loan'];
                      $table[$key]['supplement_freight_cost']   = $supplement[$value['id']]['supplement_freight_cost'];
                      $table[$key]['supplement_son_customer']   = $supplement[$value['id']]['supplement_son_customer'];
                       $table[$key]['supplement_readd_price']   = $supplement[$value['id']]['supplement_readd_price'];
                      $table[$key]['supplement_get_totoal']    = $supplement[$value['id']]['supplement_get_totoal'];
                      $table[$key]['supplement_price']   = $supplement[$value['id']]['supplement_price'];
                      $table[$key]['supplement_total_sell']   = $supplement[$value['id']]['supplement_total_sell'];
                      $table[$key]['supplement_total_profit']  = $supplement[$value['id']]['supplement_total_profit'];
                      $table[$key]['supplement_unit_profit']    = $supplement[$value['id']]['supplement_unit_profit'];
                      $table[$key]['supplement_arrive_status']    = $supplement[$value['id']]['supplement_arrive_status'];
                      $table[$key]['supplement_unit_price']    = $supplement[$value['id']]['supplement_unit_price'];
                }else{
                      $table[$key]['supplement_factory_date']   = '';
                      $table[$key]['supplement_factory_id']   = '';
                      $table[$key]['supplement_unit_price']   = '';
                      $table[$key]['supplement_store']   = '';
                      $table[$key]['supplement_tax']   = '否';
                      $table[$key]['supplement_factory_receipt']   = '';
                      $table[$key]['supplement_customer_receipt']   = '';
                      $table[$key]['supplement_customer_loan']   = '';
                      $table[$key]['supplement_freight_cost']   = '';
                      $table[$key]['supplement_son_customer']   = '';
                      $table[$key]['supplement_readd_price']   = '';
                      $table[$key]['supplement_get_totoal']   = '';
                      $table[$key]['supplement_price']   = '';
                      $table[$key]['supplement_total_sell']   = '';
                      $table[$key]['supplement_total_profit']   = '';
                      $table[$key]['supplement_unit_profit']   = '';
                      $table[$key]['supplement_arrive_status']   = '是';

                }

                if(isset($order_pay[$value['id']])){
                      $table[$key]['pay_date']   = $order_pay[$value['id']]['pay_date'];
                      $table[$key]['pay_money']   = $order_pay[$value['id']]['pay_money'];
                      $table[$key]['pay_mark']   = $order_pay[$value['id']]['pay_mark'];
                }else{
                      $table[$key]['pay_date']   = '';
                      $table[$key]['pay_money']   = '';
                      $table[$key]['pay_mark']   = '';
                }
            }

            $order_accounts_countadd =  DB::table('order_accounts_countadd')
                ->where('date',$request->date)
                ->where('supplyer_name',$request->supplyer_name)->first();
            $order_accounts_countadd = (array)$order_accounts_countadd;
            if(empty($order_accounts_countadd)){
                $order_accounts_countadd = DB::select('SHOW COLUMNS FROM re_order_accounts_countadd');
                foreach ($order_accounts_countadd as $key=>$value){
                       $value = (array)$value;
                        if(empty($order_accounts_countadd[$value['Field']])){
                            $order_accounts_countadd[$value['Field']] = '';
                        }
                }
            }

            return view('admin/finance/pay_table_detail',[
                    'order_accounts_countadd'=>$order_accounts_countadd,
                    'order'=>$table,
                    'supplyer_name'=>$request->supplyer_name,
                    'start_date'=>$request->date,
                    'end_date'=>$request->end_date
                ]);
        }



          /*
         * 处理报表详情下载
         * */
        public function get_table_detail_download(Request $request){

            $customer = DB::table('customer')
                ->select('*')
                ->where('id',$request->customer_id)
                ->first();
            $order = DB::table('order')
                ->select(
                    'id','driver_get_date','order_code','agreement_code',
                    'product_name','standards','pack_method','product_num','unit_price',
                    'total_sell','get_address','servicer_mark'
                    )
                ->where('make_date','>',$request->date)
                ->where('make_date','<',$request->end_date)
                ->where('customer_id',$request->customer_id)
                ->get();

            $order = json_decode($order,1);

            if(!empty($order)) {
                foreach ($order as $key=>$value) {
                    $order_accounts_pay = DB::table('order_accounts_pay')
                        ->select('pay_date', 'pay_money', 'pay_mark')
                        ->where('order_id', $value['id'])
                        ->first();
                    if (!empty($order_accounts_pay)) {
                        $order[$key]['pay_date'] = $order_accounts_pay->pay_date;
                        $order[$key]['pay_money'] = $order_accounts_pay->pay_money;
                        $order[$key]['pay_mark'] = $order_accounts_pay->pay_mark;
                    } else {
                        $order[$key]['pay_date'] = '';
                        $order[$key]['pay_money'] = '';
                        $order[$key]['pay_mark'] = '';
                    }
                }
            }

            $order_accounts = DB::table('order_accounts')
                ->where('customer_id',$request->customer_id)
                ->where('date',$request->date)
                ->first();
            $order_accounts = (array)$order_accounts;
            if(empty($order_accounts)){
                $order_info = DB::select('SHOW COLUMNS FROM re_order_accounts');
                foreach ($order_info as $key=>$value){
                       $value = (array)$value;
                        if(empty($order_accounts[$value['Field']])){
                            $order_accounts[$value['Field']] = '';
                        }
                }
            }
                        if(empty($order_accounts['customer_name'])){
                            $order_accounts['customer_name'] = $customer->customer_name;
                        }
                        if(empty($order_accounts['customer_id'])){
                            $order_accounts['customer_id'] = $customer->id;
                        }
                        if(empty($order_accounts['customer_register_address'])){
                            $order_accounts['customer_register_address'] = $customer->address;
                        }
                        if(empty($order_accounts['customer_contact'])){
                            $order_accounts['customer_contact'] = $customer->contact;
                        }
                        if(empty($order_accounts['customer_phone'])){
                            $order_accounts['customer_phone'] = $customer->telephone;
                        }
                        if(empty($order_accounts['customer_send'])){
                            $order_accounts['customer_send'] = $customer->contact_telephone;
                        }

                return view('admin/finance/get_table_detail_download',[
                    'order_accounts'=>$order_accounts,
                    'order'=>$order,
                    'start_date'=>$request->date,
                    'end_date'=>$request->end_date
                ]);
        }



         /*
         * 处理报表详情
         * */
        public function get_table_accounts_edite(Request $request){

                 $all = $request->all();
                 $name = $request->name;
                  //进行补充操作
                 $order_accounts = DB::table('order_accounts')
                     ->where('customer_id',$request->customer_id)
                     ->where('date',$request->date)
                     ->first();

                 if(!empty($order_accounts)){
                     DB::table('order_accounts')->where('customer_id',$request->customer_id)->update(array($name=>$request->value));
                     return 1;
                 }else{
                     var_dump($order_accounts);
                     DB::table('order_accounts')->insert(array('customer_id'=>$request->customer_id,$name=>$request->value,'date'=>$request->date));
                     return 2;
                 }


        }


        /*
         * 处理报表详情
         * */
        public function pay_table_detail_order(Request $request){

               $name = $request->name;
               if(substr($name,0,3)=='pay'){
                   $order_accounts_pay = DB::table('order_accounts_pay')
                     ->where('order_id',$request->order_id)
                     ->first();
                   if(!empty($order_accounts_pay)){
                      DB::table('order_accounts_pay')->where('order_id',$request->order_id)->update(array($name=>$request->value));
                      return 1;
                   }else{
                     //var_dump($request->all());exit;
                     DB::table('order_accounts_pay')->insert(array('order_id'=>$request->order_id,$name=>$request->value));
                     return 4;
                   }

               }else if(substr($name,0,10)=='supplement'){
                  //进行补充操作
                 $supplement_order = DB::table('supplement_order')->where('order_id',$request->order_id)->first();
                 if(!empty($supplement_order)){
                     DB::table('supplement_order')->where('order_id',$request->order_id)->update(array($name=>$request->value));
                     return 1;
                 }else{
                     var_dump($supplement_order);
                     DB::table('supplement_order')->insert(array('order_id'=>$request->order_id,$name=>$request->value));
                     return 2;
                 }

            }else{
                 DB::table('order')->where('id',$request->order_id)->update(array($name=>$request->value));
                 return 3;
            }
        }


       /*
        * 处理特殊得表
        * */
       public function pay_order_accounts_countadd(Request $request){
               $name = $request->name;
                try{
               $order_accounts_countadd = DB::table('order_accounts_countadd')->where('order_id',$request->order_id)->first();
                 if(!empty($order_accounts_countadd)){
                     DB::table('order_accounts_countadd')
                         ->where('supplyer_name',$request->supplyer_name)
                         ->where('date',$request->date)
                         ->update(array($name=>$request->value));
                     return 1;
                 }else{
                     var_dump($order_accounts_countadd);
                     DB::table('order_accounts_countadd')
                         ->insert(array('supplyer_name'=>$request->supplyer_name,$name=>$request->value,'date'=>$request->date));
                     return 2;
                 }


           }catch (Exception $e){
               echo '2';
           }

       }



         /*
         * 处理报表详情
         * */
        public function get_table_detail_order(Request $request){
               $name = $request->name;
               if(substr($name,0,3)=='pay'){
                   $order_accounts_pay = DB::table('order_accounts_pay')
                     ->where('order_id',$request->order_id)
                     ->first();
                   if(!empty($order_accounts_pay)){
                      DB::table('order_accounts_pay')->where('order_id',$request->order_id)->update(array($name=>$request->value));
                      return 1;
                   }else{
                     DB::table('order_accounts_pay')->insert(array('order_id'=>$request->order_id,$name=>$request->value));
                   }

               }else{
                 DB::table('order')->where('order_id',$request->order_id)->update(array($name=>$request->value));
               }
        }



        /*
         * 根据查找servicer或者supplyer
         * */
        public function get_user($name){

            if(Session::get('role_id') == 1 ) {
                     $servicer = DB::table('servicer')
                         ->where('status',0)
                         ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code','company_id')
                         ->get();
                 }else if(Session::get('role_id') == 2 ) {
                     $servicer = DB::table('servicer')
                         ->where('company_id',Session::get('session_id'))
                         ->where('status',0)
                         ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code','company_id')
                         ->get();
                 }

        }






}